/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Options;

import Components.CustomCanvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;

/**
 *
 * @author atxy2k
 */
public class Export {
    // Formats Support bmp, jpg, jpeg, png.
    protected CustomCanvas canvas;
    protected BufferedImage image;
    
    public Export(CustomCanvas canvas) {
        this.canvas = canvas;
        Dimension d = this.canvas.getSize();
        image = new BufferedImage(d.width, d.height, BufferedImage.TYPE_INT_RGB);
    }
    /**
     * Save the image in a url the send it.
     * @param url The url the file without extension the peference
     * @param type Type the image to export, supports bmp, jpg, jpeg and png.
     */
    protected final void save(String url, String type){
        Graphics g = image.createGraphics();
        g.setColor(Color.white);
        g.fillRect(0, 0, image.getWidth(), image.getHeight());
        canvas.paint(g);
        File file = new File(url+"."+type);
        try {
            ImageIO.write(image, type, file);
        } catch (IOException ex) {
            Logger.getLogger(Export.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    public void selector(){
        JFileChooser chooser = new JFileChooser();
        chooser.setDialogTitle("Exportar");
        int n = chooser.showSaveDialog(canvas);
        if (n== JFileChooser.APPROVE_OPTION) {
            File file = chooser.getSelectedFile();
            if (!file.exists() && !exist(file)) {
                save(file.getAbsolutePath(), "jpg");
            }else{
                if (JOptionPane.OK_OPTION == JOptionPane.showConfirmDialog(chooser, "¿Desea remplazar el archivo?", "El archivo ya existe", JOptionPane.YES_NO_OPTION)) {
                    save(file.getAbsolutePath(), "jpg");
                }else{
                    selector();
                }
            }
        }
    }
    public void selector(String type){
        JFileChooser chooser = new JFileChooser();
        chooser.setDialogTitle("Exportar");
        int n = chooser.showSaveDialog(canvas);
        if (n== JFileChooser.APPROVE_OPTION) {
            File file = chooser.getSelectedFile();
            if (!file.exists() && !exist(file)) {
                save(file.getAbsolutePath(), type);
            }else{
                if (JOptionPane.OK_OPTION == JOptionPane.showConfirmDialog(chooser, "¿Desea remplazar el archivo?", "El archivo ya existe", JOptionPane.YES_NO_OPTION)) {
                    save(file.getAbsolutePath(), type);
                }else{
                    selector();
                }
            }
        }
    }
    protected boolean exist(File file){
        String path = file.getParent()+System.getProperty("file.separator");
        String name = file.getName()+".jpg";
        File file2 = new File(path+name);
        return file2.exists();
    }
    
}
